<?php

namespace app\api\controller;

use app\admin\model\Media;
use app\admin\model\MediaLog;
use app\common\controller\Api;
use app\common\model\Category;
/**
 * 首页接口
 */
class Index extends Api
{
    protected $noNeedLogin = ['*'];
    protected $noNeedRight = ['*'];

    /**
     * 首页
     *
     */
    public function index()
    {
        $category = Category::where('pid',0)->field('id,name,image,description')->order('weigh desc,id desc')->select();
        foreach ($category as &$row) {
            switch ($row->id) {
                case 13:
                    $row->description = \app\admin\model\Story::where('status',1)->order('id','desc')->value('title');
                    break;
                case 12:
                    $row->description = \app\admin\model\Geosci::where('status',1)->order('id','desc')->value('title');
                    break;
                default:
                    $row->description = \app\admin\model\Media::where('status',1)->order('id','desc')->value('title');
                    break;
            }
        }
        $this->success('请求成功',$category);
    }

    /**
     * 数据列表
     *
     */
    public function list()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Unknown data format'));
        }
        $para = $this->request->post();
        $type = isset($para['type']) ? $para['type'] : 1;
        switch ((int)$type) {
            case 2:
                $name = 'Story';
                break;
            case 3:
                $name = 'Geosci';
                break;
            default:
                $name = 'Media';
                break;
        }
        $name = "\\app\\admin\\model\\".$name;
        $list = $name::list($para,$this->auth->id);
        $this->success('请求成功',$list);
    }

    /*
     * 查看会员未读条数
     */
    public function unread()
    {
        $para = $this->request->post();
        if (!isset($para['id']) ||!is_numeric($para['id']) || $para['id'] <= 0) {
            $this->error(__('Invalid parameters'));
        }
        $all = Media::all(['status'=>'1']);
        $num = 0;
        foreach ($all as $val){
            $check = MediaLog::get(['user_id'=>$para['id'],'media_id'=>$val['id']]);
            if(empty($check)){
                $num++;
            }
        }
        $this->success('请求成功',$num);
    }
    /**
     * 查看数据
     *
     */
    public function info()
    {
        if (!$this->request->isPost()) {
            $this->error(__('Unknown data format'));
        }
        $para = $this->request->post();
        if (!isset($para['id']) ||!is_numeric($para['id']) || $para['id'] <= 0) {
            $this->error(__('Invalid parameters'));
        }

        $type = isset($para['type']) ? $para['type'] : 1;
        switch ((int)$type) {
            case 2:
                $name = 'Story';
                break;
            case 3:
                $name = 'Geosci';
                break;
            default:
                $name = 'Media';
                break;
        }
        $name = "\\app\\admin\\model\\".$name;
        $where = [
            'id'    => $para['id'],
            'status'   => 1
        ];
        $row = $name::where($where)->find();
        if (!$row) {
            $this->error(__('No results were found'));
        }
        $name = $name."Log";
        $list = $name::look($para,$this->auth->id);
        $this->success('请求成功',$list);
    }

}
